home *** CD-ROM | disk | FTP | other *** search
/ EuroCD 3 / EuroCD 3.iso / Programming / Python1.4_Source / Python / bltinmodule.c next >
C/C++ Source or Header  |  1998-06-24  |  34KB  |  1,756 lines

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. ******************************************************************/
  31.  
  32. /* Built-in functions */
  33.  
  34. #include "allobjects.h"
  35.  
  36. #include "node.h"
  37. #include "graminit.h"
  38. #include "bltinmodule.h"
  39. #include "import.h"
  40. #include "compile.h"
  41. #include "eval.h"
  42.  
  43. #include "mymath.h"
  44.  
  45. #include "protos/bltinmodule_protos.h"
  46.  
  47. /* Forward */
  48. static object *filterstring PROTO((object *, object *));
  49. static object *filtertuple  PROTO((object *, object *));
  50.  
  51. static object *
  52. builtin___import__(self, args)
  53.     object *self;
  54.     object *args;
  55. {
  56.     char *name;
  57.     object *globals = NULL;
  58.     object *locals = NULL;
  59.     object *fromlist = NULL;
  60.  
  61.     if (!newgetargs(args, "s|OOO:__import__",
  62.             &name, &globals, &locals, &fromlist))
  63.         return NULL;
  64.     return import_module(name);
  65. }
  66.  
  67.  
  68. static object *
  69. builtin_abs(self, args)
  70.     object *self;
  71.     object *args;
  72. {
  73.     object *v;
  74.     number_methods *nm;
  75.  
  76.     if (!newgetargs(args, "O:abs", &v))
  77.         return NULL;
  78.     if ((nm = v->ob_type->tp_as_number) == NULL) {
  79.         err_setstr(TypeError, "abs() requires numeric argument");
  80.         return NULL;
  81.     }
  82.     return (*nm->nb_absolute)(v);
  83. }
  84.  
  85. static object *
  86. builtin_apply(self, args)
  87.     object *self;
  88.     object *args;
  89. {
  90.     object *func, *alist = NULL, *kwdict = NULL;
  91.  
  92.     if (!newgetargs(args, "O|OO:apply", &func, &alist, &kwdict))
  93.         return NULL;
  94.     if (alist != NULL && !is_tupleobject(alist)) {
  95.         err_setstr(TypeError, "apply() 2nd argument must be tuple");
  96.         return NULL;
  97.     }
  98.     if (kwdict != NULL && !is_dictobject(kwdict)) {
  99.         err_setstr(TypeError,
  100.                "apply() 3rd argument must be dictionary");
  101.         return NULL;
  102.     }
  103.     return PyEval_CallObjectWithKeywords(func, alist, kwdict);
  104. }
  105.  
  106. static object *
  107. builtin_callable(self, args)
  108.     object *self;
  109.     object *args;
  110. {
  111.     object *v;
  112.  
  113.     if (!newgetargs(args, "O:callable", &v))
  114.         return NULL;
  115.     return newintobject((long)callable(v));
  116. }
  117.  
  118. static object *
  119. builtin_filter(self, args)
  120.     object *self;
  121.     object *args;
  122. {
  123.     object *func, *seq, *result;
  124.     sequence_methods *sqf;
  125.     int len;
  126.     register int i, j;
  127.  
  128.     if (!newgetargs(args, "OO:filter", &func, &seq))
  129.         return NULL;
  130.  
  131.     if (is_stringobject(seq)) {
  132.         object *r = filterstring(func, seq);
  133.         return r;
  134.     }
  135.  
  136.     if (is_tupleobject(seq)) {
  137.         object *r = filtertuple(func, seq);
  138.         return r;
  139.     }
  140.  
  141.     if ((sqf = seq->ob_type->tp_as_sequence) == NULL) {
  142.         err_setstr(TypeError,
  143.                "argument 2 to filter() must be a sequence type");
  144.         goto Fail_2;
  145.     }
  146.  
  147.     if ((len = (*sqf->sq_length)(seq)) < 0)
  148.         goto Fail_2;
  149.  
  150.     if (is_listobject(seq) && seq->ob_refcnt == 1) {
  151.         INCREF(seq);
  152.         result = seq;
  153.     }
  154.     else {
  155.         if ((result = newlistobject(len)) == NULL)
  156.             goto Fail_2;
  157.     }
  158.  
  159.     for (i = j = 0; ; ++i) {
  160.         object *item, *good;
  161.         int ok;
  162.  
  163.         if ((item = (*sqf->sq_item)(seq, i)) == NULL) {
  164.             if (i < len)
  165.                 goto Fail_1;
  166.             if (err_occurred() == IndexError) {
  167.                 err_clear();
  168.                 break;
  169.             }
  170.             goto Fail_1;
  171.         }
  172.  
  173.         if (func == None) {
  174.             good = item;
  175.             INCREF(good);
  176.         }
  177.         else {
  178.             object *arg = mkvalue("(O)", item);
  179.             if (arg == NULL)
  180.                 goto Fail_1;
  181.             good = call_object(func, arg);
  182.             DECREF(arg);
  183.             if (good == NULL) {
  184.                 DECREF(item);
  185.                 goto Fail_1;
  186.             }
  187.         }
  188.         ok = testbool(good);
  189.         DECREF(good);
  190.         if (ok) {
  191.             if (j < len) {
  192.                 if (setlistitem(result, j++, item) < 0)
  193.                     goto Fail_1;
  194.             }
  195.             else {
  196.                 j++;
  197.                 if (addlistitem(result, item) < 0)
  198.                     goto Fail_1;
  199.             }
  200.         } else {
  201.             DECREF(item);
  202.         }
  203.     }
  204.  
  205.  
  206.     if (j < len && setlistslice(result, j, len, NULL) < 0)
  207.         goto Fail_1;
  208.  
  209.     return result;
  210.  
  211. Fail_1:
  212.     DECREF(result);
  213. Fail_2:
  214.     return NULL;
  215. }
  216.  
  217. static object *
  218. builtin_chr(self, args)
  219.     object *self;
  220.     object *args;
  221. {
  222.     long x;
  223.     char s[1];
  224.  
  225.     if (!newgetargs(args, "l:chr", &x))
  226.         return NULL;
  227.     if (x < 0 || x >= 256) {
  228.         err_setstr(ValueError, "chr() arg not in range(256)");
  229.         return NULL;
  230.     }
  231.     s[0] = x;
  232.     return newsizedstringobject(s, 1);
  233. }
  234.  
  235. static object *
  236. builtin_cmp(self, args)
  237.     object *self;
  238.     object *args;
  239. {
  240.     object *a, *b;
  241.  
  242.     if (!newgetargs(args, "OO:cmp", &a, &b))
  243.         return NULL;
  244.     return newintobject((long)cmpobject(a, b));
  245. }
  246.  
  247. static object *
  248. builtin_coerce(self, args)
  249.     object *self;
  250.     object *args;
  251. {
  252.     object *v, *w;
  253.     object *res;
  254.  
  255.     if (!newgetargs(args, "OO:coerce", &v, &w))
  256.         return NULL;
  257.     if (coerce(&v, &w) < 0)
  258.         return NULL;
  259.     res = mkvalue("(OO)", v, w);
  260.     DECREF(v);
  261.     DECREF(w);
  262.     return res;
  263. }
  264.  
  265. static object *
  266. builtin_compile(self, args)
  267.     object *self;
  268.     object *args;
  269. {
  270.     char *str;
  271.     char *filename;
  272.     char *startstr;
  273.     int start;
  274.  
  275.     if (!newgetargs(args, "sss:compile", &str, &filename, &startstr))
  276.         return NULL;
  277.     if (strcmp(startstr, "exec") == 0)
  278.         start = file_input;
  279.     else if (strcmp(startstr, "eval") == 0)
  280.         start = eval_input;
  281.     else if (strcmp(startstr, "single") == 0)
  282.         start = single_input;
  283.     else {
  284.         err_setstr(ValueError,
  285.            "compile() mode must be 'exec' or 'eval' or 'single'");
  286.         return NULL;
  287.     }
  288.     return compile_string(str, filename, start);
  289. }
  290.  
  291. #ifndef WITHOUT_COMPLEX
  292.  
  293. static object *
  294. builtin_complex(self, args)
  295.     object *self;
  296.     object *args;
  297. {
  298.     object *r, *i, *tmp;
  299.     number_methods *nbr, *nbi;
  300.     Py_complex cr, ci;
  301.  
  302.     i = NULL;
  303.     if (!newgetargs(args, "O|O:complex", &r, &i))
  304.         return NULL;
  305.     if ((nbr = r->ob_type->tp_as_number) == NULL ||
  306.          nbr->nb_float == NULL || (i != NULL &&
  307.        ((nbi = i->ob_type->tp_as_number) == NULL ||
  308.          nbi->nb_float == NULL))) {
  309.         err_setstr(TypeError,
  310.                "complex() argument can't be converted to complex");
  311.         return NULL;
  312.     }
  313.     if (is_complexobject(r))
  314.         cr = ((complexobject*)r)->cval;
  315.     else {
  316.         tmp = (*nbr->nb_float)(r);
  317.         if (tmp == NULL)
  318.             return NULL;
  319.         cr.real = getfloatvalue(tmp);
  320.         DECREF(tmp);
  321.         cr.imag = 0.;
  322.     }
  323.     if (i == NULL) {
  324.         ci.real = 0.;
  325.         ci.imag = 0.;
  326.     }
  327.     else if (is_complexobject(i))
  328.         ci = ((complexobject*)i)->cval;
  329.     else {
  330.         tmp = (*nbr->nb_float)(i);
  331.         if (tmp == NULL)
  332.             return NULL;
  333.         ci.real = getfloatvalue(tmp);
  334.         DECREF(tmp);
  335.         ci.imag = 0.;
  336.     }
  337.     cr.real -= ci.imag;
  338.     cr.imag += ci.real;
  339.     return newcomplexobject(cr);
  340. }
  341.  
  342. #endif
  343.  
  344. static object *
  345. builtin_dir(self, args)
  346.     object *self;
  347.     object *args;
  348. {
  349.     object *v = NULL;
  350.     object *d;
  351.  
  352.     if (!newgetargs(args, "|O:dir", &v))
  353.         return NULL;
  354.     if (v == NULL) {
  355.         d = getlocals();
  356.         INCREF(d);
  357.     }
  358.     else {
  359.         d = getattr(v, "__dict__");
  360.         if (d == NULL) {
  361.             err_setstr(TypeError,
  362.                 "dir() argument must have __dict__ attribute");
  363.             return NULL;
  364.         }
  365.     }
  366.     if (is_dictobject(d)) {
  367.         v = getdictkeys(d);
  368.         if (sortlist(v) != 0) {
  369.             DECREF(v);
  370.             v = NULL;
  371.         }
  372.     }
  373.     else {
  374.         v = PyObject_CallMethod(d, "keys", NULL);
  375.         if (v == NULL) {
  376.             PyErr_Clear();
  377.             v = newlistobject(0);
  378.         }
  379.     }
  380.     DECREF(d);
  381.     return v;
  382. }
  383.  
  384. static object *
  385. do_divmod(v, w)
  386.     object *v, *w;
  387. {
  388.     object *res;
  389.  
  390.     if (is_instanceobject(v) || is_instanceobject(w))
  391.         return instancebinop(v, w, "__divmod__", "__rdivmod__",
  392.                      do_divmod);
  393.     if (v->ob_type->tp_as_number == NULL ||
  394.                 w->ob_type->tp_as_number == NULL) {
  395.         err_setstr(TypeError,
  396.             "divmod() requires numeric or class instance arguments");
  397.         return NULL;
  398.     }
  399.     if (coerce(&v, &w) != 0)
  400.         return NULL;
  401.     res = (*v->ob_type->tp_as_number->nb_divmod)(v, w);
  402.     DECREF(v);
  403.     DECREF(w);
  404.     return res;
  405. }
  406.  
  407. static object *
  408. builtin_divmod(self, args)
  409.     object *self;
  410.     object *args;
  411. {
  412.     object *v, *w;
  413.  
  414.     if (!newgetargs(args, "OO:divmod", &v, &w))
  415.         return NULL;
  416.     return do_divmod(v, w);
  417. }
  418.  
  419. static object *
  420. builtin_eval(self, args)
  421.     object *self;
  422.     object *args;
  423. {
  424.     object *cmd;
  425.     object *globals = None, *locals = None;
  426.     char *str;
  427.  
  428.     if (!newgetargs(args, "O|O!O!:eval",
  429.             &cmd,
  430.             &Mappingtype, &globals,
  431.             &Mappingtype, &locals))
  432.         return NULL;
  433.     if (globals == None) {
  434.         globals = getglobals();
  435.         if (locals == None)
  436.             locals = getlocals();
  437.     }
  438.     else if (locals == None)
  439.         locals = globals;
  440.     if (dictlookup(globals, "__builtins__") == NULL) {
  441.         if (dictinsert(globals, "__builtins__", getbuiltins()) != 0)
  442.             return NULL;
  443.     }
  444.     if (is_codeobject(cmd))
  445.         return eval_code((codeobject *) cmd, globals, locals);
  446.     if (!is_stringobject(cmd)) {
  447.         err_setstr(TypeError,
  448.                "eval() argument 1 must be string or code object");
  449.         return NULL;
  450.     }
  451.     str = getstringvalue(cmd);
  452.     if (strlen(str) != getstringsize(cmd)) {
  453.         err_setstr(ValueError,
  454.                "embedded '\\0' in string arg");
  455.         return NULL;
  456.     }
  457.     while (*str == ' ' || *str == '\t')
  458.         str++;
  459.     return run_string(str, eval_input, globals, locals);
  460. }
  461.  
  462. static object *
  463. builtin_execfile(self, args)
  464.     object *self;
  465.     object *args;
  466. {
  467.     char *filename;
  468.     object *globals = None, *locals = None;
  469.     object *res;
  470.     FILE* fp;
  471.  
  472.     if (!newgetargs(args, "s|O!O!:execfile",
  473.             &filename,
  474.             &Mappingtype, &globals,
  475.             &Mappingtype, &locals))
  476.         return NULL;
  477.     if (globals == None) {
  478.         globals = getglobals();
  479.         if (locals == None)
  480.             locals = getlocals();
  481.     }
  482.     else if (locals == None)
  483.         locals = globals;
  484.     if (dictlookup(globals, "__builtins__") == NULL) {
  485.         if (dictinsert(globals, "__builtins__", getbuiltins()) != 0)
  486.             return NULL;
  487.     }
  488.     BGN_SAVE
  489.     fp = fopen(filename, "r");
  490.     END_SAVE
  491.     if (fp == NULL) {
  492.         err_errno(IOError);
  493.         return NULL;
  494.     }
  495.     res = run_file(fp, filename, file_input, globals, locals);
  496.     BGN_SAVE
  497.     fclose(fp);
  498.     END_SAVE
  499.     return res;
  500. }
  501.  
  502. static object *
  503. builtin_float(self, args)
  504.     object *self;
  505.     object *args;
  506. {
  507.     object *v;
  508.     number_methods *nb;
  509.  
  510.     if (!newgetargs(args, "O:float", &v))
  511.         return NULL;
  512.     if ((nb = v->ob_type->tp_as_number) == NULL ||
  513.         nb->nb_float == NULL) {
  514.         err_setstr(TypeError,
  515.                "float() argument can't be converted to float");
  516.         return NULL;
  517.     }
  518.     return (*nb->nb_float)(v);
  519. }
  520.  
  521. static object *
  522. builtin_getattr(self, args)
  523.     object *self;
  524.     object *args;
  525. {
  526.     object *v;
  527.     object *name;
  528.  
  529.     if (!newgetargs(args, "OS:getattr", &v, &name))
  530.         return NULL;
  531.     return getattro(v, name);
  532. }
  533.  
  534. static object *
  535. builtin_globals(self, args)
  536.     object *self;
  537.     object *args;
  538. {
  539.     object *d;
  540.  
  541.     if (!newgetargs(args, ""))
  542.         return NULL;
  543.     d = getglobals();
  544.     INCREF(d);
  545.     return d;
  546. }
  547.  
  548. static object *
  549. builtin_hasattr(self, args)
  550.     object *self;
  551.     object *args;
  552. {
  553.     object *v;
  554.     object *name;
  555.  
  556.     if (!newgetargs(args, "OS:hasattr", &v, &name))
  557.         return NULL;
  558.     v = getattro(v, name);
  559.     if (v == NULL) {
  560.         err_clear();
  561.         return newintobject(0L);
  562.     }
  563.     DECREF(v);
  564.     return newintobject(1L);
  565. }
  566.  
  567. static object *
  568. builtin_id(self, args)
  569.     object *self;
  570.     object *args;
  571. {
  572.     object *v;
  573.  
  574.     if (!newgetargs(args, "O:id", &v))
  575.         return NULL;
  576.     return newintobject((long)v);
  577. }
  578.  
  579. static object *
  580. builtin_map(self, args)
  581.     object *self;
  582.     object *args;
  583. {
  584.     typedef struct {
  585.         object *seq;
  586.         sequence_methods *sqf;
  587.         int len;
  588.     } sequence;
  589.  
  590.     object *func, *result;
  591.     sequence *seqs = NULL, *sqp;
  592.     int n, len;
  593.     register int i, j;
  594.  
  595.     n = gettuplesize(args);
  596.     if (n < 2) {
  597.         err_setstr(TypeError, "map() requires at least two args");
  598.         return NULL;
  599.     }
  600.  
  601.     func = gettupleitem(args, 0);
  602.     n--;
  603.  
  604.     if ((seqs = NEW(sequence, n)) == NULL) {
  605.         err_nomem();
  606.         goto Fail_2;
  607.     }
  608.  
  609.     for (len = 0, i = 0, sqp = seqs; i < n; ++i, ++sqp) {
  610.         int curlen;
  611.     
  612.         if ((sqp->seq = gettupleitem(args, i + 1)) == NULL)
  613.             goto Fail_2;
  614.  
  615.         if (! (sqp->sqf = sqp->seq->ob_type->tp_as_sequence)) {
  616.             static char errmsg[] =
  617.                 "argument %d to map() must be a sequence object";
  618.             char errbuf[sizeof(errmsg) + 3];
  619.  
  620.             sprintf(errbuf, errmsg, i+2);
  621.             err_setstr(TypeError, errbuf);
  622.             goto Fail_2;
  623.         }
  624.  
  625.         if ((curlen = sqp->len = (*sqp->sqf->sq_length)(sqp->seq)) < 0)
  626.             goto Fail_2;
  627.  
  628.         if (curlen > len)
  629.             len = curlen;
  630.     }
  631.  
  632.     if ((result = (object *) newlistobject(len)) == NULL)
  633.         goto Fail_2;
  634.  
  635.     /* XXX Special case map(None, single_list) could be more efficient */
  636.     for (i = 0; ; ++i) {
  637.         object *alist, *item, *value;
  638.         int any = 0;
  639.  
  640.         if (func == None && n == 1)
  641.             alist = NULL;
  642.         else {
  643.             if ((alist = newtupleobject(n)) == NULL)
  644.                 goto Fail_1;
  645.         }
  646.  
  647.         for (j = 0, sqp = seqs; j < n; ++j, ++sqp) {
  648.             if (sqp->len < 0) {
  649.                 INCREF(None);
  650.                 item = None;
  651.             }
  652.             else {
  653.                 item = (*sqp->sqf->sq_item)(sqp->seq, i);
  654.                 if (item == NULL) {
  655.                     if (i < sqp->len)
  656.                         goto Fail_0;
  657.                     if (err_occurred() == IndexError) {
  658.                         err_clear();
  659.                         INCREF(None);
  660.                         item = None;
  661.                         sqp->len = -1;
  662.                     }
  663.                     else {
  664.                         goto Fail_0;
  665.                     }
  666.                 }
  667.                 else
  668.                     any = 1;
  669.  
  670.             }
  671.             if (!alist)
  672.                 break;
  673.             if (settupleitem(alist, j, item) < 0) {
  674.                 DECREF(item);
  675.                 goto Fail_0;
  676.             }
  677.             continue;
  678.  
  679.         Fail_0:
  680.             XDECREF(alist);
  681.             goto Fail_1;
  682.         }
  683.  
  684.         if (!alist)
  685.             alist = item;
  686.  
  687.         if (!any) {
  688.             DECREF(alist);
  689.             break;
  690.         }
  691.  
  692.         if (func == None)
  693.             value = alist;
  694.         else {
  695.             value = call_object(func, alist);
  696.             DECREF(alist);
  697.             if (value == NULL)
  698.                 goto Fail_1;
  699.         }
  700.         if (i >= len) {
  701.             if (addlistitem(result, value) < 0)
  702.                 goto Fail_1;
  703.         }
  704.         else {
  705.             if (setlistitem(result, i, value) < 0)
  706.                 goto Fail_1;
  707.         }
  708.     }
  709.  
  710.     DEL(seqs);
  711.     return result;
  712.  
  713. Fail_1:
  714.     DECREF(result);
  715. Fail_2:
  716.     if (seqs) DEL(seqs);
  717.     return NULL;
  718. }
  719.  
  720. static object *
  721. builtin_setattr(self, args)
  722.     object *self;
  723.     object *args;
  724. {
  725.     object *v;
  726.     object *name;
  727.     object *value;
  728.  
  729.     if (!newgetargs(args, "OSO:setattr", &v, &name, &value))
  730.         return NULL;
  731.     if (setattro(v, name, value) != 0)
  732.         return NULL;
  733.     INCREF(None);
  734.     return None;
  735. }
  736.  
  737. static object *
  738. builtin_delattr(self, args)
  739.     object *self;
  740.     object *args;
  741. {
  742.     object *v;
  743.     object *name;
  744.  
  745.     if (!newgetargs(args, "OS:delattr", &v, &name))
  746.         return NULL;
  747.     if (setattro(v, name, (object *)NULL) != 0)
  748.         return NULL;
  749.     INCREF(None);
  750.     return None;
  751. }
  752.  
  753. static object *
  754. builtin_hash(self, args)
  755.     object *self;
  756.     object *args;
  757. {
  758.     object *v;
  759.     long x;
  760.  
  761.     if (!newgetargs(args, "O:hash", &v))
  762.         return NULL;
  763.     x = hashobject(v);
  764.     if (x == -1)
  765.         return NULL;
  766.     return newintobject(x);
  767. }
  768.  
  769. static object *
  770. builtin_hex(self, args)
  771.     object *self;
  772.     object *args;
  773. {
  774.     object *v;
  775.     number_methods *nb;
  776.  
  777.     if (!newgetargs(args, "O:hex", &v))
  778.         return NULL;
  779.     
  780.     if ((nb = v->ob_type->tp_as_number) == NULL ||
  781.         nb->nb_hex == NULL) {
  782.         err_setstr(TypeError,
  783.                "hex() argument can't be converted to hex");
  784.         return NULL;
  785.     }
  786.     return (*nb->nb_hex)(v);
  787. }
  788.  
  789. static object *builtin_raw_input PROTO((object *, object *));
  790.  
  791. static object *
  792. builtin_input(self, args)
  793.     object *self;
  794.     object *args;
  795. {
  796.     object *line;
  797.     char *str;
  798.     object *res;
  799.     object *globals, *locals;
  800.  
  801.     line = builtin_raw_input(self, args);
  802.     if (line == NULL)
  803.         return line;
  804.     if (!getargs(line, "s;embedded '\\0' in input line", &str))
  805.         return NULL;
  806.     while (*str == ' ' || *str == '\t')
  807.             str++;
  808.     globals = getglobals();
  809.     locals = getlocals();
  810.     if (dictlookup(globals, "__builtins__") == NULL) {
  811.         if (dictinsert(globals, "__builtins__", getbuiltins()) != 0)
  812.             return NULL;
  813.     }
  814.     res = run_string(str, eval_input, globals, locals);
  815.     DECREF(line);
  816.     return res;
  817. }
  818.  
  819. static object *
  820. builtin_int(self, args)
  821.     object *self;
  822.     object *args;
  823. {
  824.     object *v;
  825.     number_methods *nb;
  826.  
  827.     if (!newgetargs(args, "O:int", &v))
  828.         return NULL;
  829.     if ((nb = v->ob_type->tp_as_number) == NULL ||
  830.         nb->nb_int == NULL) {
  831.         err_setstr(TypeError,
  832.                "int() argument can't be converted to int");
  833.         return NULL;
  834.     }
  835.     return (*nb->nb_int)(v);
  836. }
  837.  
  838. static object *
  839. builtin_len(self, args)
  840.     object *self;
  841.     object *args;
  842. {
  843.     object *v;
  844.     long len;
  845.     typeobject *tp;
  846.  
  847.     if (!newgetargs(args, "O:len", &v))
  848.         return NULL;
  849.     tp = v->ob_type;
  850.     if (tp->tp_as_sequence != NULL) {
  851.         len = (*tp->tp_as_sequence->sq_length)(v);
  852.     }
  853.     else if (tp->tp_as_mapping != NULL) {
  854.         len = (*tp->tp_as_mapping->mp_length)(v);
  855.     }
  856.     else {
  857.         err_setstr(TypeError, "len() of unsized object");
  858.         return NULL;
  859.     }
  860.     if (len < 0)
  861.         return NULL;
  862.     else
  863.         return newintobject(len);
  864. }
  865.  
  866. static object *
  867. builtin_list(self, args)
  868.     object *self;
  869.     object *args;
  870. {
  871.     object *v;
  872.     sequence_methods *sqf;
  873.  
  874.     if (!newgetargs(args, "O:list", &v))
  875.         return NULL;
  876.     if ((sqf = v->ob_type->tp_as_sequence) != NULL) {
  877.         int n = (*sqf->sq_length)(v);
  878.         int i;
  879.         object *l;
  880.         if (n < 0)
  881.             return NULL;
  882.         l = newlistobject(n);
  883.         if (l == NULL)
  884.             return NULL;
  885.         for (i = 0; i < n; i++) {
  886.             object *item = (*sqf->sq_item)(v, i);
  887.             if (item == NULL) {
  888.                 DECREF(l);
  889.                 l = NULL;
  890.                 break;
  891.             }
  892.             setlistitem(l, i, item);
  893.         }
  894.         /* XXX Should support indefinite-length sequences */
  895.         return l;
  896.     }
  897.     err_setstr(TypeError, "list() argument must be a sequence");
  898.     return NULL;
  899. }
  900.  
  901.  
  902. static PyObject *
  903. builtin_slice(self, args)
  904.      PyObject *self;
  905.      PyObject *args;
  906. {
  907.   PyObject *start, *stop, *step;
  908.  
  909.   start = stop = step = NULL;
  910.  
  911.   if (!PyArg_ParseTuple(args, "O|OO:slice", &start, &stop, &step))
  912.     return NULL;
  913.  
  914.   /*This swapping of stop and start is to maintain compatibility with
  915.     the range builtin.*/
  916.   if (stop == NULL) {
  917.     stop = start;
  918.     start = NULL;
  919.   }
  920.   return PySlice_New(start, stop, step);
  921. }
  922.  
  923. static object *
  924. builtin_locals(self, args)
  925.     object *self;
  926.     object *args;
  927. {
  928.     object *d;
  929.  
  930.     if (!newgetargs(args, ""))
  931.         return NULL;
  932.     d = getlocals();
  933.     INCREF(d);
  934.     return d;
  935. }
  936.  
  937. static object *
  938. builtin_long(self, args)
  939.     object *self;
  940.     object *args;
  941. {
  942.     object *v;
  943.     number_methods *nb;
  944.     
  945.     if (!newgetargs(args, "O:long", &v))
  946.         return NULL;
  947.     if ((nb = v->ob_type->tp_as_number) == NULL ||
  948.         nb->nb_long == NULL) {
  949.         err_setstr(TypeError,
  950.                "long() argument can't be converted to long");
  951.         return NULL;
  952.     }
  953.     return (*nb->nb_long)(v);
  954. }
  955.  
  956. static object *
  957. min_max(args, sign)
  958.     object *args;
  959.     int sign;
  960. {
  961.     int i;
  962.     object *v, *w, *x;
  963.     sequence_methods *sq;
  964.  
  965.     if (gettuplesize(args) > 1)
  966.         v = args;
  967.     else if (!newgetargs(args, "O:min/max", &v))
  968.         return NULL;
  969.     sq = v->ob_type->tp_as_sequence;
  970.     if (sq == NULL) {
  971.         err_setstr(TypeError, "min() or max() of non-sequence");
  972.         return NULL;
  973.     }
  974.     w = NULL;
  975.     for (i = 0; ; i++) {
  976.         x = (*sq->sq_item)(v, i); /* Implies INCREF */
  977.         if (x == NULL) {
  978.             if (err_occurred() == IndexError) {
  979.                 err_clear();
  980.                 break;
  981.             }
  982.             XDECREF(w);
  983.             return NULL;
  984.         }
  985.         if (w == NULL)
  986.             w = x;
  987.         else {
  988.             if (cmpobject(x, w) * sign > 0) {
  989.                 DECREF(w);
  990.                 w = x;
  991.             }
  992.             else
  993.                 DECREF(x);
  994.         }
  995.     }
  996.     if (w == NULL)
  997.         err_setstr(ValueError, "min() or max() of empty sequence");
  998.     return w;
  999. }
  1000.  
  1001. static object *
  1002. builtin_min(self, v)
  1003.     object *self;
  1004.     object *v;
  1005. {
  1006.     return min_max(v, -1);
  1007. }
  1008.  
  1009. static object *
  1010. builtin_max(self, v)
  1011.     object *self;
  1012.     object *v;
  1013. {
  1014.     return min_max(v, 1);
  1015. }
  1016.  
  1017. static object *
  1018. builtin_oct(self, args)
  1019.     object *self;
  1020.     object *args;
  1021. {
  1022.     object *v;
  1023.     number_methods *nb;
  1024.  
  1025.     if (!newgetargs(args, "O:oct", &v))
  1026.         return NULL;
  1027.     if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL ||
  1028.         nb->nb_oct == NULL) {
  1029.         err_setstr(TypeError,
  1030.                "oct() argument can't be converted to oct");
  1031.         return NULL;
  1032.     }
  1033.     return (*nb->nb_oct)(v);
  1034. }
  1035.  
  1036. static object *
  1037. builtin_open(self, args)
  1038.     object *self;
  1039.     object *args;
  1040. {
  1041.     char *name;
  1042.     char *mode = "r";
  1043.     int bufsize = -1;
  1044.     object *f;
  1045.  
  1046.     if (!newgetargs(args, "s|si:open", &name, &mode, &bufsize))
  1047.         return NULL;
  1048.     f = newfileobject(name, mode);
  1049.     if (f != NULL)
  1050.         setfilebufsize(f, bufsize);
  1051.     return f;
  1052. }
  1053.  
  1054. static object *
  1055. builtin_ord(self, args)
  1056.     object *self;
  1057.     object *args;
  1058. {
  1059.     char c;
  1060.  
  1061.     if (!newgetargs(args, "c:ord", &c))
  1062.         return NULL;
  1063.     return newintobject((long)(c & 0xff));
  1064. }
  1065.  
  1066. static object *
  1067. do_pow(v, w)
  1068.     object *v, *w;
  1069. {
  1070.     object *res;
  1071.     if (is_instanceobject(v) || is_instanceobject(w))
  1072.         return instancebinop(v, w, "__pow__", "__rpow__", do_pow);
  1073.     if (v->ob_type->tp_as_number == NULL ||
  1074.         w->ob_type->tp_as_number == NULL) {
  1075.         err_setstr(TypeError, "pow() requires numeric arguments");
  1076.         return NULL;
  1077.     }
  1078.     if (
  1079. #ifndef WITHOUT_COMPLEX
  1080.             !is_complexobject(v) && 
  1081. #endif
  1082.             is_floatobject(w) && getfloatvalue(v) < 0.0) {
  1083.         if (!err_occurred())
  1084.             err_setstr(ValueError, "negative number to float power");
  1085.         return NULL;
  1086.     }
  1087.     if (coerce(&v, &w) != 0)
  1088.         return NULL;
  1089.     res = (*v->ob_type->tp_as_number->nb_power)(v, w, None);
  1090.     DECREF(v);
  1091.     DECREF(w);
  1092.     return res;
  1093. }
  1094.  
  1095. static object *
  1096. builtin_pow(self, args)
  1097.     object *self;
  1098.     object *args;
  1099. {
  1100.     object *v, *w, *z = None, *res;
  1101.     object *v1, *z1, *w2, *z2;
  1102.  
  1103.     if (!newgetargs(args, "OO|O:pow", &v, &w, &z))
  1104.         return NULL;
  1105.     if (z == None)
  1106.         return do_pow(v, w);
  1107.     /* XXX The ternary version doesn't do class instance coercions */
  1108.     if (is_instanceobject(v))
  1109.         return v->ob_type->tp_as_number->nb_power(v, w, z);
  1110.     if (v->ob_type->tp_as_number == NULL ||
  1111.         z->ob_type->tp_as_number == NULL ||
  1112.         w->ob_type->tp_as_number == NULL) {
  1113.         err_setstr(TypeError, "pow() requires numeric arguments");
  1114.         return NULL;
  1115.     }
  1116.     if (coerce(&v, &w) != 0)
  1117.         return NULL;
  1118.     res = NULL;
  1119.     v1 = v;
  1120.     z1 = z;
  1121.     if (coerce(&v1, &z1) != 0)
  1122.         goto error2;
  1123.     w2 = w;
  1124.     z2 = z1;
  1125.      if (coerce(&w2, &z2) != 0)
  1126.         goto error1;
  1127.     res = (*v1->ob_type->tp_as_number->nb_power)(v1, w2, z2);
  1128.     DECREF(w2);
  1129.     DECREF(z2);
  1130.  error1:
  1131.     DECREF(v1);
  1132.     DECREF(z1);
  1133.  error2:
  1134.     DECREF(v);
  1135.     DECREF(w);
  1136.     return res;
  1137. }
  1138.  
  1139. static object *
  1140. builtin_range(self, args)
  1141.     object *self;
  1142.     object *args;
  1143. {
  1144.     long ilow = 0, ihigh = 0, istep = 1;
  1145.     int i, n;
  1146.     object *v;
  1147.  
  1148.     if (gettuplesize(args) <= 1) {
  1149.         if (!newgetargs(args,
  1150.                 "l;range() requires 1-3 int arguments",
  1151.                 &ihigh))
  1152.             return NULL;
  1153.     }
  1154.     else {
  1155.         if (!newgetargs(args,
  1156.                 "ll|l;range() requires 1-3 int arguments",
  1157.                 &ilow, &ihigh, &istep))
  1158.             return NULL;
  1159.     }
  1160.     if (istep == 0) {
  1161.         err_setstr(ValueError, "zero step for range()");
  1162.         return NULL;
  1163.     }
  1164.     /* XXX ought to check overflow of subtraction */
  1165.     if (istep > 0)
  1166.         n = (ihigh - ilow + istep - 1) / istep;
  1167.     else
  1168.         n = (ihigh - ilow + istep + 1) / istep;
  1169.     if (n < 0)
  1170.         n = 0;
  1171.     v = newlistobject(n);
  1172.     if (v == NULL)
  1173.         return NULL;
  1174.     for (i = 0; i < n; i++) {
  1175.         object *w = newintobject(ilow);
  1176.         if (w == NULL) {
  1177.             DECREF(v);
  1178.             return NULL;
  1179.         }
  1180.         setlistitem(v, i, w);
  1181.         ilow += istep;
  1182.     }
  1183.     return v;
  1184. }
  1185.  
  1186. static object *
  1187. builtin_xrange(self, args)
  1188.     object *self;
  1189.     object *args;
  1190. {
  1191.     long ilow = 0, ihigh = 0, istep = 1;
  1192.     long n;
  1193.  
  1194.     if (gettuplesize(args) <= 1) {
  1195.         if (!newgetargs(args,
  1196.                 "l;xrange() requires 1-3 int arguments",
  1197.                 &ihigh))
  1198.             return NULL;
  1199.     }
  1200.     else {
  1201.         if (!newgetargs(args,
  1202.                 "ll|l;xrange() requires 1-3 int arguments",
  1203.                 &ilow, &ihigh, &istep))
  1204.             return NULL;
  1205.     }
  1206.     if (istep == 0) {
  1207.         err_setstr(ValueError, "zero step for xrange()");
  1208.         return NULL;
  1209.     }
  1210.     /* XXX ought to check overflow of subtraction */
  1211.     if (istep > 0)
  1212.         n = (ihigh - ilow + istep - 1) / istep;
  1213.     else
  1214.         n = (ihigh - ilow + istep + 1) / istep;
  1215.     if (n < 0)
  1216.         n = 0;
  1217.     return newrangeobject(ilow, n, istep, 1);
  1218. }
  1219.  
  1220. extern char *my_readline PROTO((char *));
  1221.  
  1222. static object *
  1223. builtin_raw_input(self, args)
  1224.     object *self;
  1225.     object *args;
  1226. {
  1227.     object *v = NULL;
  1228.     object *f;
  1229.  
  1230.     if (!newgetargs(args, "|O:[raw_]input", &v))
  1231.         return NULL;
  1232.     if (getfilefile(sysget("stdin")) == stdin &&
  1233.         getfilefile(sysget("stdout")) == stdout &&
  1234.         isatty(fileno(stdin)) && isatty(fileno(stdout))) {
  1235.         object *po;
  1236.         char *prompt;
  1237.         char *s;
  1238.         object *result;
  1239.         if (v != NULL) {
  1240.             po = strobject(v);
  1241.             if (po == NULL)
  1242.                 return NULL;
  1243.             prompt = getstringvalue(po);
  1244.         }
  1245.         else {
  1246.             po = NULL;
  1247.             prompt = "";
  1248.         }
  1249.         s = my_readline(prompt);
  1250.         XDECREF(po);
  1251.         if (s == NULL) {
  1252.             err_set(KeyboardInterrupt);
  1253.             return NULL;
  1254.         }
  1255.         if (*s == '\0') {
  1256.             err_set(EOFError);
  1257.             result = NULL;
  1258.         }
  1259.         else { /* strip trailing '\n' */
  1260.             result = newsizedstringobject(s, strlen(s)-1);
  1261.         }
  1262.         free(s);
  1263.         return result;
  1264.     }
  1265.     if (v != NULL) {
  1266.         f = sysget("stdout");
  1267.         if (f == NULL) {
  1268.             err_setstr(RuntimeError, "lost sys.stdout");
  1269.             return NULL;
  1270.         }
  1271.         flushline();
  1272.         if (writeobject(v, f, PRINT_RAW) != 0)
  1273.             return NULL;
  1274.     }
  1275.     f = sysget("stdin");
  1276.     if (f == NULL) {
  1277.         err_setstr(RuntimeError, "lost sys.stdin");
  1278.         return NULL;
  1279.     }
  1280.     return filegetline(f, -1);
  1281. }
  1282.  
  1283. static object *
  1284. builtin_reduce(self, args)
  1285.     object *self;
  1286.     object *args;
  1287. {
  1288.     object *seq, *func, *result = NULL;
  1289.     sequence_methods *sqf;
  1290.     register int i;
  1291.  
  1292.     if (!newgetargs(args, "OO|O:reduce", &func, &seq, &result))
  1293.         return NULL;
  1294.     if (result != NULL)
  1295.         INCREF(result);
  1296.  
  1297.     if ((sqf = seq->ob_type->tp_as_sequence) == NULL) {
  1298.         err_setstr(TypeError,
  1299.             "2nd argument to reduce() must be a sequence object");
  1300.         return NULL;
  1301.     }
  1302.  
  1303.     if ((args = newtupleobject(2)) == NULL)
  1304.         goto Fail;
  1305.  
  1306.     for (i = 0; ; ++i) {
  1307.         object *op2;
  1308.  
  1309.         if (args->ob_refcnt > 1) {
  1310.             DECREF(args);
  1311.             if ((args = newtupleobject(2)) == NULL)
  1312.                 goto Fail;
  1313.         }
  1314.  
  1315.         if ((op2 = (*sqf->sq_item)(seq, i)) == NULL) {
  1316.             if (err_occurred() == IndexError) {
  1317.                 err_clear();
  1318.                 break;
  1319.             }
  1320.             goto Fail;
  1321.         }
  1322.  
  1323.         if (result == NULL)
  1324.             result = op2;
  1325.         else {
  1326.             settupleitem(args, 0, result);
  1327.             settupleitem(args, 1, op2);
  1328.             if ((result = call_object(func, args)) == NULL)
  1329.                 goto Fail;
  1330.         }
  1331.     }
  1332.  
  1333.     DECREF(args);
  1334.  
  1335.     if (result == NULL)
  1336.         err_setstr(TypeError,
  1337.                "reduce of empty sequence with no initial value");
  1338.  
  1339.     return result;
  1340.  
  1341. Fail:
  1342.     XDECREF(args);
  1343.     XDECREF(result);
  1344.     return NULL;
  1345. }
  1346.  
  1347. static object *
  1348. builtin_reload(self, args)
  1349.     object *self;
  1350.     object *args;
  1351. {
  1352.     object *v;
  1353.  
  1354.     if (!newgetargs(args, "O:reload", &v))
  1355.         return NULL;
  1356.     return reload_module(v);
  1357. }
  1358.  
  1359. static object *
  1360. builtin_repr(self, args)
  1361.     object *self;
  1362.     object *args;
  1363. {
  1364.     object *v;
  1365.  
  1366.     if (!newgetargs(args, "O:repr", &v))
  1367.         return NULL;
  1368.     return reprobject(v);
  1369. }
  1370.  
  1371. static object *
  1372. builtin_round(self, args)
  1373.     object *self;
  1374.     object *args;
  1375. {
  1376.     double x;
  1377.     double f;
  1378.     int ndigits = 0;
  1379.     int i;
  1380.  
  1381.     if (!newgetargs(args, "d|i:round", &x, &ndigits))
  1382.             return NULL;
  1383.     f = 1.0;
  1384.     for (i = ndigits; --i >= 0; )
  1385.         f = f*10.0;
  1386.     for (i = ndigits; ++i <= 0; )
  1387.         f = f*0.1;
  1388.     if (x >= 0.0)
  1389.         return newfloatobject(floor(x*f + 0.5) / f);
  1390.     else
  1391.         return newfloatobject(ceil(x*f - 0.5) / f);
  1392. }
  1393.  
  1394. static object *
  1395. builtin_str(self, args)
  1396.     object *self;
  1397.     object *args;
  1398. {
  1399.     object *v;
  1400.  
  1401.     if (!newgetargs(args, "O:str", &v))
  1402.         return NULL;
  1403.     return strobject(v);
  1404. }
  1405.  
  1406. static object *
  1407. builtin_tuple(self, args)
  1408.     object *self;
  1409.     object *args;
  1410. {
  1411.     object *v;
  1412.     sequence_methods *sqf;
  1413.  
  1414.     if (!newgetargs(args, "O:tuple", &v))
  1415.         return NULL;
  1416.     if (is_tupleobject(v)) {
  1417.         INCREF(v);
  1418.         return v;
  1419.     }
  1420.     if (is_listobject(v))
  1421.         return listtuple(v);
  1422.     if (is_stringobject(v)) {
  1423.         int n = getstringsize(v);
  1424.         object *t = newtupleobject(n);
  1425.         if (t != NULL) {
  1426.             int i;
  1427.             char *p = getstringvalue(v);
  1428.             for (i = 0; i < n; i++) {
  1429.                 object *item = newsizedstringobject(p+i, 1);
  1430.                 if (item == NULL) {
  1431.                     DECREF(t);
  1432.                     t = NULL;
  1433.                     break;
  1434.                 }
  1435.                 settupleitem(t, i, item);
  1436.             }
  1437.         }
  1438.         return t;
  1439.     }
  1440.     /* Generic sequence object */
  1441.     if ((sqf = v->ob_type->tp_as_sequence) != NULL) {
  1442.         int n = (*sqf->sq_length)(v);
  1443.         int i;
  1444.         object *t;
  1445.         if (n < 0)
  1446.             return NULL;
  1447.         t = newtupleobject(n);
  1448.         if (t == NULL)
  1449.             return NULL;
  1450.         for (i = 0; i < n; i++) {
  1451.             object *item = (*sqf->sq_item)(v, i);
  1452.             if (item == NULL) {
  1453.                 DECREF(t);
  1454.                 t = NULL;
  1455.                 break;
  1456.             }
  1457.             settupleitem(t, i, item);
  1458.         }
  1459.         /* XXX Should support indefinite-length sequences */
  1460.         return t;
  1461.     }
  1462.     /* None of the above */
  1463.     err_setstr(TypeError, "tuple() argument must be a sequence");
  1464.     return NULL;
  1465. }
  1466.  
  1467. static object *
  1468. builtin_type(self, args)
  1469.     object *self;
  1470.     object *args;
  1471. {
  1472.     object *v;
  1473.  
  1474.     if (!newgetargs(args, "O:type", &v))
  1475.         return NULL;
  1476.     v = (object *)v->ob_type;
  1477.     INCREF(v);
  1478.     return v;
  1479. }
  1480.  
  1481. static object *
  1482. builtin_vars(self, args)
  1483.     object *self;
  1484.     object *args;
  1485. {
  1486.     object *v = NULL;
  1487.     object *d;
  1488.  
  1489.     if (!newgetargs(args, "|O:vars", &v))
  1490.         return NULL;
  1491.     if (v == NULL) {
  1492.         d = getlocals();
  1493.         if (d == NULL) {
  1494.             if (!err_occurred())
  1495.                 err_setstr(SystemError, "no locals!?");
  1496.         }
  1497.         else
  1498.             INCREF(d);
  1499.     }
  1500.     else {
  1501.         d = getattr(v, "__dict__");
  1502.         if (d == NULL) {
  1503.             err_setstr(TypeError,
  1504.                 "vars() argument must have __dict__ attribute");
  1505.             return NULL;
  1506.         }
  1507.     }
  1508.     return d;
  1509. }
  1510.  
  1511. static struct methodlist builtin_methods[] = {
  1512.     {"__import__",    builtin___import__, 1},
  1513.     {"abs",        builtin_abs, 1},
  1514.     {"apply",    builtin_apply, 1},
  1515.     {"callable",    builtin_callable, 1},
  1516.     {"chr",        builtin_chr, 1},
  1517.     {"cmp",        builtin_cmp, 1},
  1518.     {"coerce",    builtin_coerce, 1},
  1519.     {"compile",    builtin_compile, 1},
  1520. #ifndef WITHOUT_COMPLEX
  1521.     {"complex",    builtin_complex, 1},
  1522. #endif
  1523.     {"delattr",    builtin_delattr, 1},
  1524.     {"dir",        builtin_dir, 1},
  1525.     {"divmod",    builtin_divmod, 1},
  1526.     {"eval",    builtin_eval, 1},
  1527.     {"execfile",    builtin_execfile, 1},
  1528.     {"filter",    builtin_filter, 1},
  1529.     {"float",    builtin_float, 1},
  1530.     {"getattr",    builtin_getattr, 1},
  1531.     {"globals",    builtin_globals, 1},
  1532.     {"hasattr",    builtin_hasattr, 1},
  1533.     {"hash",    builtin_hash, 1},
  1534.     {"hex",        builtin_hex, 1},
  1535.     {"id",        builtin_id, 1},
  1536.     {"input",    builtin_input, 1},
  1537.     {"int",        builtin_int, 1},
  1538.     {"len",        builtin_len, 1},
  1539.     {"list",    builtin_list, 1},
  1540.     {"locals",    builtin_locals, 1},
  1541.     {"long",    builtin_long, 1},
  1542.     {"map",        builtin_map, 1},
  1543.     {"max",        builtin_max, 1},
  1544.     {"min",        builtin_min, 1},
  1545.     {"oct",        builtin_oct, 1},
  1546.     {"open",    builtin_open, 1},
  1547.     {"ord",        builtin_ord, 1},
  1548.     {"pow",        builtin_pow, 1},
  1549.     {"range",    builtin_range, 1},
  1550.     {"raw_input",    builtin_raw_input, 1},
  1551.     {"reduce",    builtin_reduce, 1},
  1552.     {"reload",    builtin_reload, 1},
  1553.     {"repr",    builtin_repr, 1},
  1554.     {"round",    builtin_round, 1},
  1555.     {"setattr",    builtin_setattr, 1},
  1556.     {"slice",       builtin_slice, 1},
  1557.     {"str",        builtin_str, 1},
  1558.     {"tuple",    builtin_tuple, 1},
  1559.     {"type",    builtin_type, 1},
  1560.     {"vars",    builtin_vars, 1},
  1561.     {"xrange",    builtin_xrange, 1},
  1562.     {NULL,        NULL},
  1563. };
  1564.  
  1565. static object *builtin_mod;
  1566. static object *builtin_dict;
  1567.  
  1568. object *
  1569. getbuiltinmod()
  1570. {
  1571.     return builtin_mod;
  1572. }
  1573.  
  1574. object *
  1575. getbuiltindict()
  1576. {
  1577.     return builtin_dict;
  1578. }
  1579.  
  1580. /* Predefined exceptions */
  1581.  
  1582. object *AccessError;
  1583. object *AttributeError;
  1584. object *ConflictError;
  1585. object *EOFError;
  1586. object *IOError;
  1587. object *ImportError;
  1588. object *IndexError;
  1589. object *KeyError;
  1590. object *KeyboardInterrupt;
  1591. object *MemoryError;
  1592. object *NameError;
  1593. object *OverflowError;
  1594. object *RuntimeError;
  1595. object *SyntaxError;
  1596. object *SystemError;
  1597. object *SystemExit;
  1598. object *TypeError;
  1599. object *ValueError;
  1600. object *ZeroDivisionError;
  1601.  
  1602. static object *
  1603. newstdexception(name)
  1604.     char *name;
  1605. {
  1606.     object *v = newstringobject(name);
  1607.     if (v == NULL || dictinsert(builtin_dict, name, v) != 0)
  1608.         fatal("no mem for new standard exception");
  1609.     return v;
  1610. }
  1611.  
  1612. static void
  1613. initerrors()
  1614. {
  1615.     AccessError = newstdexception("AccessError");
  1616.     AttributeError = newstdexception("AttributeError");
  1617.     ConflictError = newstdexception("ConflictError");
  1618.     EOFError = newstdexception("EOFError");
  1619.     IOError = newstdexception("IOError");
  1620.     ImportError = newstdexception("ImportError");
  1621.     IndexError = newstdexception("IndexError");
  1622.     KeyError = newstdexception("KeyError");
  1623.     KeyboardInterrupt = newstdexception("KeyboardInterrupt");
  1624.     MemoryError = newstdexception("MemoryError");
  1625.     NameError = newstdexception("NameError");
  1626.     OverflowError = newstdexception("OverflowError");
  1627.     RuntimeError = newstdexception("RuntimeError");
  1628.     SyntaxError = newstdexception("SyntaxError");
  1629.     SystemError = newstdexception("SystemError");
  1630.     SystemExit = newstdexception("SystemExit");
  1631.     TypeError = newstdexception("TypeError");
  1632.     ValueError = newstdexception("ValueError");
  1633.     ZeroDivisionError = newstdexception("ZeroDivisionError");
  1634. }
  1635.  
  1636. void
  1637. initbuiltin()
  1638. {
  1639.     builtin_mod = initmodule("__builtin__", builtin_methods);
  1640.     builtin_dict = getmoduledict(builtin_mod);
  1641.     INCREF(builtin_dict);
  1642.     initerrors();
  1643.     (void) dictinsert(builtin_dict, "None", None);
  1644.     (void) dictinsert(builtin_dict, "Ellipsis", Py_Ellipsis);
  1645. }
  1646.  
  1647.  
  1648. /* Helper for filter(): filter a tuple through a function */
  1649.  
  1650. static object *
  1651. filtertuple(func, tuple)
  1652.     object *func;
  1653.     object *tuple;
  1654. {
  1655.     object *result;
  1656.     register int i, j;
  1657.     int len = gettuplesize(tuple);
  1658.  
  1659.     if (len == 0) {
  1660.         INCREF(tuple);
  1661.         return tuple;
  1662.     }
  1663.  
  1664.     if ((result = newtupleobject(len)) == NULL)
  1665.         return NULL;
  1666.  
  1667.     for (i = j = 0; i < len; ++i) {
  1668.         object *item, *good;
  1669.         int ok;
  1670.  
  1671.         if ((item = gettupleitem(tuple, i)) == NULL)
  1672.             goto Fail_1;
  1673.         if (func == None) {
  1674.             INCREF(item);
  1675.             good = item;
  1676.         }
  1677.         else {
  1678.             object *arg = mkvalue("(O)", item);
  1679.             if (arg == NULL)
  1680.                 goto Fail_1;
  1681.             good = call_object(func, arg);
  1682.             DECREF(arg);
  1683.             if (good == NULL)
  1684.                 goto Fail_1;
  1685.         }
  1686.         ok = testbool(good);
  1687.         DECREF(good);
  1688.         if (ok) {
  1689.             INCREF(item);
  1690.             if (settupleitem(result, j++, item) < 0)
  1691.                 goto Fail_1;
  1692.         }
  1693.     }
  1694.  
  1695.     if (resizetuple(&result, j, 0) < 0)
  1696.         return NULL;
  1697.  
  1698.     return result;
  1699.  
  1700. Fail_1:
  1701.     DECREF(result);
  1702.     return NULL;
  1703. }
  1704.  
  1705.  
  1706. /* Helper for filter(): filter a string through a function */
  1707.  
  1708. static object *
  1709. filterstring(func, strobj)
  1710.     object *func;
  1711.     object *strobj;
  1712. {
  1713.     object *result;
  1714.     register int i, j;
  1715.     int len = getstringsize(strobj);
  1716.  
  1717.     if (func == None) {
  1718.         /* No character is ever false -- share input string */
  1719.         INCREF(strobj);
  1720.         return strobj;
  1721.     }
  1722.     if ((result = newsizedstringobject(NULL, len)) == NULL)
  1723.         return NULL;
  1724.  
  1725.     for (i = j = 0; i < len; ++i) {
  1726.         object *item, *arg, *good;
  1727.         int ok;
  1728.  
  1729.         item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i);
  1730.         if (item == NULL)
  1731.             goto Fail_1;
  1732.         arg = mkvalue("(O)", item);
  1733.         DECREF(item);
  1734.         if (arg == NULL)
  1735.             goto Fail_1;
  1736.         good = call_object(func, arg);
  1737.         DECREF(arg);
  1738.         if (good == NULL)
  1739.             goto Fail_1;
  1740.         ok = testbool(good);
  1741.         DECREF(good);
  1742.         if (ok)
  1743.             GETSTRINGVALUE((stringobject *)result)[j++] =
  1744.                 GETSTRINGVALUE((stringobject *)item)[0];
  1745.     }
  1746.  
  1747.     if (j < len && resizestring(&result, j) < 0)
  1748.         return NULL;
  1749.  
  1750.     return result;
  1751.  
  1752. Fail_1:
  1753.     DECREF(result);
  1754.     return NULL;
  1755. }
  1756.